package com.yhp.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class EncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        request.setCharacterEncoding("utf-8");

        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String uri = request.getRequestURI();
       if (uri.contains("/login.jsp") || uri.contains("/login") || uri.contains("/css/") || uri.contains("/js/") || uri.contains("/images/")) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            Object name = request.getSession().getAttribute("username");
            if (name == null) {
                System.out.println(11111);
                request.getRequestDispatcher("login.jsp").forward(request, response);
            }
            filterChain.doFilter(servletRequest, servletResponse);
            System.out.println("结束1");
        }
    }


    @Override
    public void destroy() {

    }
}
